<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>我的头有点痒</title>
</head>

<body>
    <!-- 右上角笑脸刷新页面 -->
    <div class="emoji-wrapper">
        &#x1F60D;
    </div>

    <!-- 搜索框 -->
    <div class="search-bar">
        <form action="#">
            <input type="text" placeholder="搜索...">
            <button type="submit">搜索</button>
        </form>
    </div>
    <!-- 顶部导航 -->
    <header>
        <nav class="top-navigation">
            <ul>
                <li><a href="#">通知</a></li>
                <li><a href="#">私信</a></li>
                <div class="dropdown">
                    <button class="dropbtn" id="submitBtn">投稿</button>
                    <div class="dropdown-content" id="myDropdown">
                        <a href="#">发布视频</a>
                        <a href="#">视频管理</a>
                        <a href="chuang.html">创作中心</a>
                    </div>
                </div>
                <button id="toggleAuthBox" onclick="showLoginBox()">登录/注册</button>
            </ul>
        </nav>
    </header>

    <!-- 侧栏导航 -->
    <aside class="sidebar-navigation">
        <ul>
            <li><a href="#">产品1</a></li>
            <li><a href="#">产品2</a></li>
            <li><a href="#">产品3</a></li>
            <li><a href="#">常见问题</a></li>
        </ul>
    </aside>

    <!-- 主内容区域 -->
    <main>
        <h1>欢迎来到我们的网站</h1>
        <p>这里是主要内容...</p>
    </main>


    <?php
    // 连接数据库
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "userdb";
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // 检查连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
    
    // 处理登录
    if (isset($_POST['login'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
    
        // 查询数据库
        $sql = "SELECT * FROM users WHERE username = '$username'";
        $result = $conn->query($sql);
    
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            // 使用哈希比较密码，这里简化了密码验证过程
            if (password_verify($password, $row['password_hash'])) {
                // 登录成功，可以设置session等
                $message = "登录成功";
                // 添加此行来指示登录成功后应关闭登录框
    $closeLoginBox = true;
            } else {
                $message = "密码错误";
            }
        } else {
            $message = "用户不存在";
        }
    }

    
    
    // 处理注册
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['register'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $confirmPassword = $_POST['confirmPassword'];

        // 检查两次输入的密码是否一致
        if ($password !== $confirmPassword) {
            $message = "两次密码不一致";
        } else {
            // 检查用户名是否已存在
            $sql = "SELECT * FROM users WHERE username = '$username'";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                $message = "用户名已存在!";
            } else {
                // 使用密码哈希
                $password_hash = password_hash($password, PASSWORD_DEFAULT);

                // 插入新用户
                $sql = "INSERT INTO users (username, password_hash) VALUES ('$username', '$password_hash')";

                if ($conn->query($sql) === TRUE) {
                    $message = "注册成功！请登录";
                } else {
                    $message = "注册失败: " . $conn->error;
                }
            }
        }
    }
}
    $conn->close();
    
    ?>
    
    <!-- 登录表单 -->
    <div class="login-box">
        <span class="close-btn" onclick="closeLoginOrRegisterBox()">×</span>
        <h2>登录</h2>
        <form method="post">
            <input type="text" name="username" placeholder="用户名" required>
            <input type="password" name="password" placeholder="密码" required>
            <button type="submit" name="login">登录</button>
            <br><br>
            <a href="#" onclick="showRegisterBox(); return false;">没有账号？去注册</a>
        </form>
    </div>
    
    <!-- 注册表单 -->
    <div class="register-box" style="display: none;">
        <span class="close-btn" onclick="closeLoginOrRegisterBox()">×</span>
        <h2>注册</h2>
        <form method="post">
            <input type="text" id="regUsername" name="username" placeholder="用户名" required>
            <input type="password" id="regPassword" name="password" placeholder="密码" required>
            <input type="password" id="confirmPassword" name="confirmPassword" placeholder="再次输入密码" required>
            <button type="submit" name="register">注册</button>
            <br><br>
            <a href="#" onclick="showLoginBox(); return false;">已有账号？去登录</a>
        </form>
    </div>

    <script src="scripts.js"></script>

    <!-- 注册消息弹窗 -->
<script>
    window.onload = function() {
    var message = "<?php echo isset($message) ? addslashes($message) : ''; ?>";
    var closeLoginBox = "<?php echo isset($closeLoginBox) && $closeLoginBox ? 'true' : 'false'; ?>";
    
    if (message) {
        alert(message);
    }
    
    // 如果登录成功，关闭登录窗口
    if (closeLoginBox === 'true') {
        closeLoginOrRegisterBox(); // 调用原函数
    }
};
</script>